#############################################################

import re

    # + -- 1 or more occurrences of the pattern to its left, e.g. 'i+' = one or more i's
    # * -- 0 or more occurrences of the pattern to its left
    # ? -- match 0 or 1 occurrences of the pattern to its left
    # (.*?) NON-GREEDY QUANTIFICATION
    # re.I Performs case-insensitive matching.

line = "Cats ARE smarter than dogs"

matchObj = re.match( r'(.*) are (.*?) .*', line, re.I)

if matchObj:
   print "matchObj.group() : ",  matchObj.group()
   print "matchObj.group(1) : ", matchObj.group(1)
   print "matchObj.group(2) : ", matchObj.group(2)
else:
   print "No match!!"

#############################################################
   
line = "Cats are smarter than dogs";

searchObj = re.search( r'(.*) are (.*?) .*', line, re.I)

if searchObj:
   print "searchObj.group() : ", searchObj.group()
   print "searchObj.group(1) : ", searchObj.group(1)
   print "searchObj.group(2) : ", searchObj.group(2)
else:
   print "Nothing found!!"

#############################################################

#What is the diffrence MATCH and SEARCH?

# match checks for a match only at the beginning of the string, while search checks for a
# match anywhere in the string (this is what Perl does by default).

#############################################################

re.match("c", "abcdef")  # No match
re.search("c", "abcdef") # Match

#############################################################

phone = "2004-959-559 # This is Phone Number"

# Delete Python-style comments
num = re.sub(r'#.*', "", phone)
print "Phone Num : ", num

# Remove anything other than digits \D Matches nondigits.
num = re.sub(r'\D', "", phone)    
print "Phone Num : ", num

#############################################################

#retun list - in this case word with two char
print re.findall(".{2}","this is a sample text")

############################################################
str = "This is a sample test text";
print str.split();       #split by space ['This', 'is', 'a', 'sample', 'test', 'text']
print str.split(' ', 2); #split by space * 2 only ['This', 'is', 'a sample test text']

############################################################

pattern = r'(\d+).(\d*)'
str     = '342.79+12.56'

r       = re.compile(pattern)
m       = r.match(str)

print m.group()
print m.group(1)
print m.group(2)

if m:
    print("{0} pasuje do {1}".format(pattern, str))
else:
    print("{0} nie pasuje do {1}".format(pattern, str))


